import axios from 'axios';
import query from 'query-string';

const BASE_URL = '/product/spu';


export interface SpuParam {
    name?: string;
    page?: number;
    size?: number;
    sort?: Array<string>;
}

export interface SpuResponse {
    id?: number;
    name: string;
    subName?: string;
    defaultPrice?: string;
    defaultPic?: string;
    categoryId?: string;
    parentCategoryId?: string;
    brandId?: string;
    brandName?: string;
    deleteStatus?: string;
    publishStatus?: string;
    verifyStatus?: string;
    packageList?: string;
    defaultSkuId?: string;

    createdBy?: string;
    updatedBy?: string;
    createdAt?: string;
    updatedAt?: string;
}

export interface SpuSaveCommand {
    id?: number;
    name: string;
    subName?: string;
    defaultPrice?: string;
    defaultPic?: string;
    categoryId?: string;
    parentCategoryId?: string;
    brandId?: string;
    brandName?: string;
    deleteStatus?: string;
    publishStatus?: string;
    verifyStatus?: string;
    packageList?: string;
    defaultSkuId?: string;

    attributeKeyIdList?: number[],
}

export interface SpuResponseList {
    list: SpuResponse[];
    total: number;
}


// 通过SpuParam参数获取分页列表
export function pageSpu(params: SpuParam) {
    return axios.get<SpuResponseList>(`${BASE_URL}/page`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    })
}

// 通过id获取spu信息
export function getSpu(id: number) {
    return axios.get<SpuResponse>(`${BASE_URL}/${id}`);
    
}

export function listSpu(params: SpuParam) {
    return axios.get<SpuResponse[]>(`${BASE_URL}/list`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    });
}

// 添加spu
export function addSpu(params: SpuSaveCommand) {
    return axios.post(`${BASE_URL}/add`, params);
}

// 更新spu
export function updateSpu(params: SpuSaveCommand) {
    return axios.put(`${BASE_URL}/update`, params);
}

// 批量删除spu
export function deleteSpu(ids: number | Array<number>) {
    return axios.delete(`${BASE_URL}/delete/${ids}`);
}